package com.alibaba.service;

import com.alibaba.bean.*;
import com.alibaba.mapper.NoteListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class NoteListService {

    @Autowired
    private NoteListMapper noteListMapper;

    public NoteListResult getNoteList(NoteList noteList) {
        NoteListResult nlr = new NoteListResult();
        DailyList dl = new DailyList();
        DailyListData dld = new DailyListData();
        ReplyList rl = new ReplyList();
        ReplyListData rld = new ReplyListData();
        int notelow = (noteList.getPage() - 1) * 20;
        int notehigh = (noteList.getPage() * 20) - 1;
        nlr.setResult(false);

        try{
            if (noteList.isFollow()) {
                int i,j;
                List<DailyListData> dldlist = (List<DailyListData>) noteListMapper.noteFollow(noteList.getUserId(), notelow, notehigh);
                //List<ReplyListData> rldlist = (List<ReplyListData>) noteListMapper.noteReply(noteList.getUserId(), notelow, notehigh);
                for (i = 0; i < dldlist.size(); i++) {
                    String str = noteListMapper.img(dldlist.get(i).getForumId());
                    dld.setImgList(str);
                    dldlist.get(i).setImgList(dld.getImgList());
                    dldlist.get(i).setFollow(true);

                    int x=dldlist.get(i).getForumId();
                    AuthorInfo auInfo = noteListMapper.info(x);
                    dldlist.get(i).setAuthorInfo(auInfo);

                    if (noteListMapper.checkLike(noteList.getUserId(), dldlist.get(i).getForumId()) != null) {
                         if (dldlist.get(i).getForumId() == noteListMapper.checkLike(noteList.getUserId(), dldlist.get(i).getForumId()).getForumId()) {
                             dldlist.get(i).setLike(true);
                        } else {
                            dldlist.get(i).setLike(false);
                        }
                    } else {
                        dldlist.get(i).setLike(false);
                    }

                    List<ReplyListData> rldlist = (List<ReplyListData>) noteListMapper.noteReply(dldlist.get(i).getForumId(), notelow, notehigh);
                    j=0;
                    while(j<rldlist.size()) {
                        if (rldlist.get(j).getTouserId() == 0){
                            String s1 = auInfo.getUserName();
                            rldlist.get(j).setTouserName(s1);
                        } else {
                            String sss = noteListMapper.rName(rldlist.get(j).getTouserId());
                            rldlist.get(j).setTouserName(sss);
                        }
                        j++;
                    }
                    dldlist.get(i).setReplyList(rldlist);
                }
                dl.setDailyList(dldlist);
                nlr.setData(dl);
                nlr.setResult(true);
                nlr.setErrorMsg("关注列表成功");
            } else {
                int m,n;
                List<DailyListData> dldlist2 = (List<DailyListData>) noteListMapper.noteNew(notelow, notehigh);
                //List<ReplyListData> rldlist2 = (List<ReplyListData>) noteListMapper.noteReply(noteList.getUserId(), notelow, notehigh);
                for (m=0;m<dldlist2.size();m++) {
                    String str = noteListMapper.img(dldlist2.get(m).getForumId());
                    dld.setImgList(str);
                    dldlist2.get(m).setImgList(dld.getImgList());

                    if (dldlist2.get(m).getForumLike() != 0) {
                        dldlist2.get(m).setLike(true);
                    } else {
                        dldlist2.get(m).setLike(false);
                    }

                    int x=dldlist2.get(m).getForumId();
                    AuthorInfo auInfo2 = noteListMapper.info(x);
                    dldlist2.get(m).setAuthorInfo(auInfo2);

                    if (noteListMapper.checkLike(noteList.getUserId(), dldlist2.get(m).getForumId()) != null) {
                        if (dldlist2.get(m).getForumId() == noteListMapper.checkLike(noteList.getUserId(), dldlist2.get(m).getForumId()).getForumId()) {
                            dldlist2.get(m).setLike(true);
                        } else {
                            dldlist2.get(m).setLike(false);
                        }
                    } else {
                        dldlist2.get(m).setLike(false);
                    }

                    if (noteListMapper.checkFollow(dldlist2.get(m).getForumId(), noteList.getUserId()) != null) {
                           if (auInfo2.getUserId() == noteListMapper.checkFollow(dldlist2.get(m).getForumId(), noteList.getUserId()).getUserId()) {
                            dldlist2.get(m).setFollow(true);
                        } else {
                            dldlist2.get(m).setFollow(false);
                        }
                    } else {
                        dldlist2.get(m).setFollow(false);
                    }

                    List<ReplyListData> rldlist2 = (List<ReplyListData>) noteListMapper.noteReply(dldlist2.get(m).getForumId(), notelow, notehigh);
                    n=0;
                    while(n<rldlist2.size()) {
                        if (rldlist2.get(n).getTouserId() == 0){
                            String s1 = auInfo2.getUserName();
                            rldlist2.get(n).setTouserName(s1);
                        } else {
                            String sss = noteListMapper.rName(rldlist2.get(n).getTouserId());
                            rldlist2.get(n).setTouserName(sss);
                        }
                        n++;
                    }
                    dldlist2.get(m).setReplyList(rldlist2);         //xxx
                }
                dl.setDailyList(dldlist2);
                nlr.setData(dl);
                nlr.setResult(true);
                nlr.setErrorMsg("新帖列表成功");
            }
        } catch (Exception e) {
            nlr.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return nlr;
    }

}
